package com.goktech.sportsmall.service.Impl;


import com.goktech.sportsmall.bean.CommSortBean;
import com.goktech.sportsmall.bean.OrderBean;
import com.goktech.sportsmall.bean.OrderDetailsBean;
import com.goktech.sportsmall.mapper.CommSortMapper;
import com.goktech.sportsmall.mapper.OrderDetailsMapper;
import com.goktech.sportsmall.mapper.OrderMapper;
import com.goktech.sportsmall.mapper.ShoppingCartMapper;
import com.goktech.sportsmall.service.OrderService;
import com.goktech.sportsmall.utils.DateUtil;
import com.goktech.sportsmall.utils.DynamicNumberUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class OrderServiceImpl implements OrderService {
    /*
    1.查询用户对应的购物车信息
    2.根据购物车中的商品信息逐一判断库存
    3.创建订单
    4.创建订单详情
    5.扣除库存信息
    6.删除购物车中的数据
    */
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailsMapper orderDetialsMapper;
    @Autowired
    private CommSortMapper commSortMapper;

    @Override
    public OrderBean createOrder(String userId, String name, String phone, String address) {
        List<CommSortBean> commSortBeanList = shoppingCartMapper.selectByUId(userId);
        // 根据购物车重点额商品注意判断库存
        for(int i=0;i<commSortBeanList.size();i++){
            CommSortBean commSortBean = commSortBeanList.get(i);
            // 判断购物车中的商品的购买数量是否大于库存数量，大于库存数量则不进行购买，从当前的集合中去除
            if(commSortBean.getCs_rep()<commSortBean.getSc_number()){
                // 从 List集合中去除某一个元素
                commSortBeanList.remove(commSortBean);
            }
        }
        // 创建订单
        String orderNo = orderMapper.selectByCreateTime(DateUtil.getDate("yyyyMMdd"));
        /*
            订单编号的创建规则: O+年月日+四位数字
         * */
        String oNo = "O"+ DateUtil.getDate("yyyyMMdd");
        if (orderNo ==null){
            oNo = oNo+"0001";
        }else{
            String str = orderNo.substring(9);//急缺编码后四位
            int no = Integer.valueOf(str); //
            no++;
            oNo = oNo + DynamicNumberUtil.fillStr(4,no); // 进行自动不全，对齐到4位
        }
        OrderBean orderBean = new OrderBean();
        orderBean.setONo(oNo);
        orderBean.setONo(oNo);
        orderBean.setOCreateTime(DateUtil.getDate("yyyyMMddHHmmss"));
        orderBean.setOUserName(name);
        orderBean.setOUserAddress(address);
        orderBean.setOUserPhone(phone);
        orderBean.setOState(1);
        orderMapper.insertOrder(orderBean);
        // 创建叮当详情
        OrderDetailsBean orderDetailsBean;
        for (CommSortBean commSortBean:commSortBeanList){
            orderDetailsBean = new OrderDetailsBean();
            orderDetailsBean.setOdCommName(commSortBean.getC_name());
            orderDetailsBean.setOdCommSort(commSortBean.getC_sort());
            orderDetailsBean.setOdImg(commSortBean.getCs_img());
            orderDetailsBean.setOdNumber(commSortBean.getSc_number());
            orderDetailsBean.setOdPrice(commSortBean.getCs_price());
            orderDetialsMapper.insertOrderDetails(orderDetailsBean);
            // 修改库存
            commSortMapper.updateRep(orderDetailsBean.getOdNumber()+"",commSortBean.getCs_no());
            // 删除购物车中的数据
            shoppingCartMapper.deleteByNo(commSortBean.getSc_id());
        }


        return orderBean;
    }
}
